package com.example.war.kaptcha;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.text.impl.DefaultWordRenderer;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Properties;

/**
 * @author void
 * @date 2021/5/19 9:50
 * @desc
 */
@Controller
public class ImgController {
    @Autowired
    private DefaultKaptcha defaultKaptcha;

    /**
     * 图片验证码接口
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @throws Exception
     */
    @GetMapping("/pic")
    public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        try {
            // 生产验证码字符串并保存到session中
            httpServletResponse.setDateHeader("Expires", 0);
            httpServletResponse.setHeader("Pragma", "no-cache");
            httpServletResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
            httpServletResponse.addHeader("Cache-Control", "post-check=0, pre-check=0");
            httpServletResponse.setContentType("image/jpeg");
            //向客户端写出
            BufferedImage bi = defaultKaptcha.createImage("张三");
            ServletOutputStream out = httpServletResponse.getOutputStream();
            ImageIO.write(bi, "jpg", out);
        } catch (IllegalArgumentException e) {
            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
    }

    public static void main(String[] args) throws IOException {
        //https://my.oschina.net/dreamnight/blog/695042
        Properties properties = new Properties();
        //图片宽
        properties.put(Constants.KAPTCHA_IMAGE_WIDTH,"110");
        //图片高
        properties.put(Constants.KAPTCHA_IMAGE_HEIGHT,"60");
        //字体大小
        properties.put(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE,"50");
        properties.put(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, "red");
        //宋体,楷体,微软雅黑
        properties.put(Constants.KAPTCHA_TEXTPRODUCER_FONT_NAMES, "楷体");
        //验证码文本字符长度
        properties.put(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");

//        properties.put(Constants.KAPTCHA_NOISE_IMPL, NoNoise.class.getName());
//        properties.put(Constants.KAPTCHA_OBSCURIFICATOR_IMPL, MyFontStyle.class.getName());

//        properties.put(Constants.KAPTCHA_BACKGROUND_IMPL, TranslucentBackground.class.getName());

        Config config = new Config(properties);


        DefaultWordRenderer wordRenderer = new DefaultWordRenderer();
        wordRenderer.setConfig(config);
        BufferedImage bi = wordRenderer.renderWord("罗志祥", 110, 60);
        File out = new File("D://imgs/kaptcha.png");
        ImageIO.write(bi, "png", out);
    }
}
